<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px">
      <el-form-item label="Email" prop="email1">
        <el-input
          v-model="queryParams.email1"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Name" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Phone" prop="phone1">
        <el-input
          v-model="queryParams.phone1"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Salary" prop="salary">
        <el-input
          style="width: 80px;margin-right: 10px;"
          v-model="queryParams.minSalary"
          placeholder="From"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <span>-</span>
        <el-input
          style="width: 80px;margin-left: 10px;"
          v-model="queryParams.maxSalary"
          placeholder="To"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="Age"
        prop="age">
        <el-input
          style="width: 100px;margin-right: 10px;"
          v-model="queryParams.minAge"
          placeholder="From"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <span>-</span>
        <el-input
          style="width: 100px;margin-left: 10px;"
          v-model="queryParams.maxAge"
          placeholder="To"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        v-hasPermi="['system:candidate:staff']"
        prop="createUserid"
        label="Staff">
        <el-select
          clearable
          filterable
          remote
          :remote-method="filterVmModel"
          v-selectLoadmore="loadMore"
          v-model="queryParams.createUserid"
          :loading="loadingStaff"
          placeholder="">
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="Birthyear" prop="birthyear">-->
        <!--<el-input-->
          <!--v-model="queryParams.birthyear"-->
          <!--placeholder=""-->
          <!--clearable-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <!--<el-button v-if="false" icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >Add</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['system:candidate:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['system:candidate:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
          <!--v-hasPermi="['system:candidate:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="candidateList"
      @selection-change="handleSelectionChange">
      <el-table-column
        width="120px;"
        label="Action"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >Edit</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message"
            @click="handleMail(scope.row)"
          >Mail</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['system:candidate:del']"
            @click="handleDelete(scope.row)"
          >Del</el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="$auth.hasPermi('system:candidate:staff')"
        width="100px"
        label="Staff"
        align="left"
        prop="createUserName" />
      <el-table-column show-overflow-tooltip label="Name" align="left" prop="name" />
      <el-table-column show-overflow-tooltip label="Company" align="left" prop="ccompanyList[0].com" />
      <el-table-column show-overflow-tooltip label="Position" align="left" prop="ccompanyList[0].title" />
      <el-table-column show-overflow-tooltip label="Education" align="left" prop="schoolList[0].name" />
      <el-table-column show-overflow-tooltip width="60px" label="Age" align="left" prop="age" />
      <el-table-column show-overflow-tooltip label="District" align="left" prop="area">
        <template slot-scope="scope">
          <span v-if="scope.row.area == 'HK'">{{scope.row.district}}</span>
          <!--<span v-else-if="scope.row.gender == 'F'">女</span>-->
          <span v-else>{{scope.row.cnAddr}}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="salary" align="left" prop="salary" />
      <!--<el-table-column :show-overflow-tooltip="true" label="District" align="left" prop="district" />-->


      <!--<el-table-column :show-overflow-tooltip="true" label="Gender" align="center" prop="gender">-->
        <!--<template slot-scope="scope">-->
          <!--<span v-if="scope.row.gender == 'M'">男</span>-->
          <!--<span v-else-if="scope.row.gender == 'F'">女</span>-->
          <!--<span v-else>未知</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column show-overflow-tooltip label="UpdateTime" align="left" prop="updateTime" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCandidate, getCandidate, delCandidate, addCandidate, updateCandidate } from "@/api/system/candidate";
import { listUser } from "@/api/system/user";
import { getToken } from "@/utils/auth";
import { area } from '@/utils/area';
import { CodeToText } from 'element-china-area-data';

export default {
  name: "Candidate",
  dicts: ['sys_hk_district', 'sys_mhk_district', 'sys_user_sex'],
  data() {
    return {
      loadingStaff: false,
      userOptions: [],
      userList: [],
      userTotal: 0,
      // 日期范围
      dateRange: [],
      queryParamsUser: {
        pageNum: 1,
        pageSize: 10,
        userName: null
      },
      optionsCity: [], //选中的省市区
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      cid: 0,
      // 【请填写功能名称】表格数据
      candidateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openJobs: false,

      queryParamsJob:{
        cid: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        email1: null,
        email2: null,
        name: null,
        phone1: null,
        phone2: null,
        gender: null,
        salary: null,
        minSalary: null,
        maxSalary: null,
        education: null,
        other: null,
        district: null,
        age: null,
        minAge: null,
        maxAge: null,
        jobFunction: null,
        birthyear: null,
        createUserid: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getUserList();
    this.getList();
  },
  methods: {
    loadMore(firstTag) {
      this.queryParamsUser.pageNum++;
      this.getUserList();
    },
    filterVmModel (value) {
      this.loadingStaff = true;
      console.log("=======================" + value)
      this.queryParamsUser.userName = value;
      this.queryParamsUser.pageNum = 1;
      this.getUserList();
    },
    getUserList() {
      //获取用户Staff
      listUser(this.addDateRange(this.queryParamsUser, this.dateRange)).then(response => {
          this.userList = response.rows;
          if (this.queryParamsUser.pageNum == 1) {
            this.userOptions = [];
            this.userOptions.push(...this.userList);
          } else {
            this.userOptions.push(...this.userList);
          }
          this.userTotal = response.total;
        }
      ).finally(f => {
        this.loadingStaff = false;
      });
    },
    getCodeToText(codeArray) {
      let area = "";
      switch (codeArray.length) {
        case 1:
          area += CodeToText[codeArray[0]];
          break;
        case 2:
          area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]];
          break;
        case 3:
          area +=
            CodeToText[codeArray[0]] +
            "/" +
            CodeToText[codeArray[1]] +
            "/" +
            CodeToText[codeArray[2]];
          break;
        default:
          break;
      }
      // console.log(area);
      return area;
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listCandidate(this.queryParams).then(response => {
        this.candidateList = response.rows;
        this.total = response.total;
        if (this.candidateList != null && this.candidateList.length > 0) {
          for (let i = 0; i < this.candidateList.length; i++) {
            if (this.candidateList[i].area == "HK") {

            } else {
              // console.log(this.candidateList[i].cnAddr.split(","))
              if (this.candidateList[i].cnAddr != null &&
                this.candidateList[i].cnAddr != undefined &&
                this.candidateList[i].cnAddr != '') {
                let cnArr = this.candidateList[i].cnAddr.split(",");
                if (cnArr != null && cnArr.length >= 3) {
                  this.candidateList[i].cnAddr = this.getCodeToText(cnArr);
                }
              }
            }
          }
        }
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        email1: null,
        email2: null,
        name: null,
        phone1: null,
        phone2: null,
        gender: null,
        salary: null,
        education: null,
        other: null,
        district: null,
        age: null,
        birthyear: null,
        ccompanyList: [],
        schoolList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const obj = { path: "/system/candidate/candidateAdd", name: "CandidateAdd" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/candidate/candidateAdd', query: {type: 2}})
      }, 300);
    },
    handleMail(row) {
      const id = row.id || this.ids
      const obj = { path: "/system/mail/mailSend", name: "MailSend" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/mail/mailSend',query: {id: id}})
      }, 300);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // 关闭指定页签
      const id = row.id || this.ids
      const obj = { path: "/system/candidate/candidateAdd", name: "CandidateAdd" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/candidate/candidateAdd',query: {id: id, type: 2}})
      }, 300);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCandidate(this.form).then(response => {
              this.$modal.msgSuccess("success");
              this.open = false;
              this.getList();
            });
          } else {
            addCandidate(this.form).then(response => {
              this.$modal.msgSuccess("success");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('Delete？').then(function() {
        return delCandidate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("success");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/candidate/export', {
        ...this.queryParams
      }, `candidate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
</style>
